package com.module.recall.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;

import com.module.recall.vo.Tdcode;

public class SecondCodeDao extends GenericHibernateDao{
	static SecondCodeDao dao;
	public SecondCodeDao(){
		
	}
	public synchronized static SecondCodeDao getInstance()
    {
        if (dao == null)
        {
            dao = new SecondCodeDao();
        }
        return dao;
    }
	
	
	public void updateFirstId(Connection con,String code,int firstid)throws SQLException{
		 PreparedStatement ps = null;
		 try {
			String sql =" update code_secondcode set i_first_id =?  where c_code_info=? ";
			 con.setAutoCommit(false);
			 ps=con.prepareStatement(sql);
			 ps.setInt(1, firstid);
			 ps.setString(2, code);
			 ps.execute();
		} finally{
			close(ps);
		}
	}
	
	//查询代是否存在
	public boolean selectByCode(Session session,String code)throws SQLException{
		boolean flag = false;
		Query query = session.createQuery(" select count(*) from SecondCode  as a where a.codeInfo='"+code+"'");
		long n=((Long)query.uniqueResult()).longValue();
		if(n==0){
			flag=false;//不存在
		}else if(n==1){
			flag=true; //存在
		}
		return flag;
	}
	
	
	
	/**
	 * 根据code获得一层码关联id
	 * @param code
	 * @return
	 */
	public int selectFirstIdByCode(Session session,String code)throws SQLException{
		int id=0;
		Query query = session.createQuery(" select a.firstId from SecondCode  as a where a.codeInfo='"+code+"'");
		List list=query.list();
		if(list!=null&&list.size()>0){
			id=Integer.parseInt(list.get(0).toString());
		}
		return id;
	}
	/**
	 * 根据关联id获得双层码号
	 * @return
	 */
	public String slectCodeInfoByFirstId(Session session,int firstId)throws SQLException{
		String str="";
		Query query = session.createQuery("select a.codeInfo from SecondCode as a where a.firstId="+firstId);
		List list=query.list();
		if(list!=null&&list.size()>0){
			str=list.get(0).toString();
		}
		return str;
	}
	
	//插入二层码
	public void insertSecondCode(Connection con,Tdcode tdcode,String codeinfo) throws SQLException{
		PreparedStatement ps = null;
		final String sqlStr=" insert into code_secondcode(c_code_info,c_sysbatch) values(?,?) ";
		if(tdcode!=null){
			try {
				ps=con.prepareStatement(sqlStr);
			    ps.setString(1, codeinfo);
			    ps.setString(2, tdcode.getBatchNum());
			    ps.execute();
			} finally{
				close(ps);
			}
		}
	}
	
	
	/**
	 * 根据批次查询信息
	 */
	@SuppressWarnings("unchecked")
	public List<String> findCodeByBatch(Session session,String batch,int start, int limit)throws SQLException{
		String hql = "select codeInfo from SecondCode where sysbatch = ?";
		Query query = session.createQuery(hql).setParameter(0, batch);
		query.setFirstResult(start).setMaxResults(limit);
		return query.list();
	}
	
	public static void main(String[] args) {

	}
	
	public List selectCodeAndIdByRollNum(Session session,String rollNum)throws SQLException{
		String hql =" select firstId,codeInfo from SecondCode b where b.firstId in(select id from FirstCode a where a.rollNum = ? order by sequenceNum asc)";
		Query query = session.createQuery(hql).setParameter(0, rollNum);
		return query.list();
	}
}
